/*
 * Easy Home Accounting
 * Copyright (c) Martin Kumst 2011
 * License: GNU GPL VERSION 2
 */

package easyhomeaccounting;
import javax.swing.*;
import javax.swing.table.*;

public class MainWindow extends javax.swing.JFrame {

    /** Creates new form MainWindow */
    public MainWindow() {
        initComponents();

        /* Vytvoreni noveho projektu */
        project = new EHAProject();

        /* Refreshovani stavu nekterych ovladacich prvku */
        updateInfoLabel();
        refreshMoveTable();

        /* Tabulka zarovnana doprava */
        makeTableAligned();

        /* Maximalizace */
        this.setVisible(true);
        this.setExtendedState(javax.swing.JFrame.MAXIMIZED_BOTH);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenuItem7 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem4 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Easy Home Accounting - Jednoduche domaci ucetnictvi - Sireno pod licenci GNU/GPL verze 2");

        jLabel1.setText("Jmeno otevreneho projektu:");

        jLabel2.setText("Zatim zadnej");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Datum", "Popis", "Prijem", "Vydaj"
            }
        ));
        jTable1.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jTable1);

        jMenu1.setText("Program");

        jMenuItem1.setText("Vytvorit novy projekt");
        jMenu1.add(jMenuItem1);

        jMenuItem2.setText("Otevrit projekt");
        jMenu1.add(jMenuItem2);

        jMenuItem3.setText("Ulozit projekt");
        jMenu1.add(jMenuItem3);

        jMenuItem5.setText("Konec");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem5);

        jMenuBar1.add(jMenu1);

        jMenu3.setText("Polozka");

        jMenuItem6.setText("Pridat novou polozku");
        jMenu3.add(jMenuItem6);

        jMenuItem7.setText("Smazat vybranou polozku");
        jMenu3.add(jMenuItem7);

        jMenuBar1.add(jMenu3);

        jMenu2.setText("Pomoc");

        jMenuItem4.setText("O programu");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem4);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE)
                .addGap(107, 107, 107))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 579, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 548, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /* Okno O programu */
    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
        showAboutProgramWindow();
    }//GEN-LAST:event_jMenuItem4ActionPerformed

    /* Konec programu */
    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
        processEndOfProgram();
    }//GEN-LAST:event_jMenuItem5ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainWindow().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

    private EHAProject project; /* Otevreny projekt */
    
    /* Zobrazeni okna O programu */
    private void showAboutProgramWindow() {
        AboutWindow aboutWindow = new AboutWindow();
        aboutWindow.setVisible(true);
    }

    /* Zpracovani konce programu (z menu) */
    private void processEndOfProgram() {
        int chosenOne = JOptionPane.showConfirmDialog(this, "Opravdu si prejete ukoncit beh programu? Kazdym ukoncenim totiz zemre jedno mlade delfina skakaveho. ;-)", "Ukonceni programu", JOptionPane.YES_NO_OPTION);
        if (chosenOne == JOptionPane.YES_OPTION) {
            System.exit(0);
        }
    }

    /* Obnoveni tabulky s polozkami */
    private void refreshMoveTable() {

        /* Ziskani modelu tabulky */
        DefaultTableModel model = (DefaultTableModel) jTable1.getModel();

        /* Pridani radku pro pridani noveho financniho pohybu */
        EHAFirstMoneyMove firstRow = new EHAFirstMoneyMove();
        model.addRow(firstRow.genMoveRow());
    }



    /* Aktualizace popisu informacniho popisku jLabel2 - informuje o jmenu souboru projektu, ktery je otevren */
    private void updateInfoLabel() {
//        if (openedProjectFilename == null) {
//            jLabel2.setText("Zadny projekt dosud nebyl otevren");
//        }
//        else {
//            jLabel2.setText(openedProjectFilename);
//        }
    }

    /* Zarovna tabulku s financnimi pohyby doprava */
    private void makeTableAligned() {

        TableColumnModel columnModel = jTable1.getColumnModel();

        for (int i = 0; i < 4; i++) {
            TableColumn column = columnModel.getColumn(i);
            DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
            renderer.setHorizontalAlignment(JLabel.RIGHT);
            column.setCellRenderer(renderer);
        }
    }
}
